'use strict';
const dayjs = require("dayjs");

module.exports = app => {
    const { STRING, INTEGER, DATE, TEXT, JSON } = app.Sequelize;
    const Message = app.model.define(
        'Message',
        {
            id: { type: INTEGER, primaryKey: true, autoIncrement: true },
            conversation_id: { type: INTEGER, allowNull: false },
            role: { type: STRING(20), allowNull: false, defaultValue: 'user' }, // user, assistant, system
            reasoning: { type: TEXT },
            answer: { type: TEXT },
            content: { type: TEXT },
            task_id: STRING,
            custom_id: STRING,
            response: JSON,
            create_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            update_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            }
        },
        {
            tableName: 'message',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );

    Message.associate = function() {
        app.model.Message.belongsTo(app.model.Conversation, { foreignKey: 'conversation_id' });
    };

    return Message;
};

// CREATE TABLE `message` (
//   `id` int(11) NOT NULL AUTO_INCREMENT,
//   `conversation_id` int(11) NOT NULL COMMENT '会话ID',
//   `role` varchar(20) NOT NULL DEFAULT 'user' COMMENT '角色类型',
//   `reasoning` text COMMENT '推理过程',
//   `answer` text COMMENT '答案',
//   `content` text NOT NULL COMMENT '消息内容',
//   `create_time` datetime DEFAULT NULL COMMENT '创建时间',
//   `update_time` datetime DEFAULT NULL COMMENT '更新时间',
//   PRIMARY KEY (`id`)
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会话消息记录表';
